/*
 Copyright 2011 Google, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

package com.adwhirl.adapters;

import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.adwhirl.AdWhirlLayout;
import com.adwhirl.AdWhirlLayout.ViewAdRunnable;
import com.adwhirl.AdWhirlTargeting;
import com.adwhirl.obj.Ration;
import com.adwhirl.util.AdWhirlUtil;
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class GoogleAdMobAdsAdapter extends AdWhirlAdapter implements AdListener {
	private AdView adView;

	public GoogleAdMobAdsAdapter(final AdWhirlLayout adWhirlLayout,
			final Ration ration) {
		super(adWhirlLayout, ration);
	}

	protected String birthdayForAdWhirlTargeting() {
		return (AdWhirlTargeting.getBirthDate() != null) ? new SimpleDateFormat(
				"yyyyMMdd").format(AdWhirlTargeting.getBirthDate().getTime())
				: null;
	}

	protected AdRequest.Gender genderForAdWhirlTargeting() {
		switch (AdWhirlTargeting.getGender()) {
		case MALE:
			return AdRequest.Gender.MALE;
		case FEMALE:
			return AdRequest.Gender.FEMALE;
		default:
			return null;
		}
	}

	@Override
	public void handle() {
		final AdWhirlLayout adWhirlLayout = adWhirlLayoutReference.get();

		if (adWhirlLayout == null) {
			return;
		}

		final Activity activity = adWhirlLayout.activityReference.get();
		if (activity == null) {
			return;
		}

		adView = new AdView(activity, AdSize.BANNER, ration.key);

		adView.setAdListener(this);
		adView.loadAd(requestForAdWhirlLayout(adWhirlLayout));
	}

	@Override
	public void willDestroy() {
		log("AdView will get destroyed");
		if (adView != null) {
			adView.destroy();
		}
	}

	protected void log(final String message) {
		Log.d(AdWhirlUtil.ADWHIRL, "GoogleAdapter " + message);
	}

	protected AdRequest requestForAdWhirlLayout(final AdWhirlLayout layout) {
		final AdRequest result = new AdRequest();

		if (AdWhirlTargeting.getTestMode()) {
			final Activity activity = layout.activityReference.get();
			if (activity != null) {
				final Context context = activity.getApplicationContext();
				final String deviceId = AdWhirlUtil.getEncodedDeviceId(context);
				result.addTestDevice(deviceId);
			}
		}
		result.setGender(genderForAdWhirlTargeting());
		result.setBirthday(birthdayForAdWhirlTargeting());

		if (layout.extra.locationOn == 1) {
			result.setLocation(layout.adWhirlManager.location);
		}

		result.setKeywords(AdWhirlTargeting.getKeywordSet());

		return result;
	}

	@Override
	public void onDismissScreen(final Ad arg0) {
	}

	@Override
	public void onFailedToReceiveAd(final Ad arg0, final ErrorCode arg1) {
		log("failure (" + arg1 + ")");

		arg0.setAdListener(null);

		final AdWhirlLayout adWhirlLayout = adWhirlLayoutReference.get();

		if (adWhirlLayout == null) {
			return;
		}
		onTheFetchFailure(arg1);
		adWhirlLayout.rollover();
	}

	@Override
	public void onLeaveApplication(final Ad arg0) {
	}

	@Override
	public void onPresentScreen(final Ad arg0) {
	}

	@Override
	public void onReceiveAd(final Ad arg0) {
		log("success");

		final AdWhirlLayout adWhirlLayout = adWhirlLayoutReference.get();

		if (adWhirlLayout == null) {
			return;
		}

		if (!(arg0 instanceof AdView)) {
			log("invalid AdView");
			return;
		}

		// IC
		onTheReceiveAd(arg0);

		final AdView adView = (AdView) arg0;

		adWhirlLayout.adWhirlManager.resetRollover();
		adWhirlLayout.handler.post(new ViewAdRunnable(adWhirlLayout, adView));
		adWhirlLayout.rotateThreadedDelayed();
	}
}
